package controller;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;

import testtimer.TestTimerBeanRemote;

import dao.InterviewDAORemote;
import dao.TestQuestionDAORemote;
import dto.QuestionDTO;

@ManagedBean
@SessionScoped
public class QuestionController implements Serializable {

	private static final long serialVersionUID = 1L;

	@EJB
	TestQuestionDAORemote qdr;

	@EJB
	InterviewDAORemote idr;

	@EJB
	TestTimerBeanRemote ttbr;

	@ManagedProperty(value = "#{questiondto}")
	QuestionDTO questiondto;
	
	@ManagedProperty(value = "#{candidateHistoryController}")
	CandidateHistoryController candidateHistoryController;

	private int page;
	private int firstpage = 0;
	private int displayedPage = 1;
	private ArrayList<String> checkbox1;
	private ArrayList<String> checkbox2;
	private ArrayList<String> checkbox3;
	private ArrayList<String> checkbox4;
	private List<Integer> marked;
	private HtmlDataTable dataTable;
	boolean ok;
	private boolean goToReview = false;
	boolean haveMarked;
	boolean dontHaveMarked;
	private boolean theEnd = true;
	private boolean notTheEnd = false;
	private String done = "button";
	private String mark = "button";
	private String pagebottom;
	private String inputHidden;
	private int nrOfMinutes;
	
	public QuestionController() {
		setMarked(new ArrayList<Integer>());
	}

	@PostConstruct
	public void start() {
		
		System.out.println(candidateHistoryController.getCurrentInterviewId());
		qdr.startTest(candidateHistoryController.getCurrentInterviewId());
		setNrOfMinutes(idr.getInterviewDuration(candidateHistoryController
				.getCurrentInterviewId()));
		ttbr.startTimer(candidateHistoryController.getCurrentInterviewId());
		System.out.println("Interview id: "
				+ candidateHistoryController.getCurrentInterviewId());
	}

	public QuestionDTO getQuestionByPageNumber(int page) {
		return qdr.getQuestionByPageNumber(page, candidateHistoryController.getCurrentInterviewId());
	}

	public void setQuestiondto(QuestionDTO questiondto) {
		this.questiondto = questiondto;
	}

	public QuestionDTO getQuestiondto() {
		// hard coded???
		return qdr.getQuestionByPageNumber(displayedPage - 1, candidateHistoryController.getCurrentInterviewId());
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPage() {
		return page;
	}

	public void incPage() {
		if (page < qdr.numberOfQuestions(candidateHistoryController.getCurrentInterviewId()) - 1) {
			page++;
			displayedPage++;
		} else {
			theEnd = false;
			notTheEnd = true;
			setDone("buttonDis");

		}
		checkbox1 = null;
		checkbox2 = null;
		checkbox3 = null;
		checkbox4 = null;
	}

	public void setFirstpage(int firstpage) {
		this.firstpage = firstpage;
	}

	public int getFirstpage() {
		return firstpage;
	}

	public void setDisplayedPage(int displayedPage) {
		this.displayedPage = displayedPage;
	}

	public int getDisplayedPage() {
		return displayedPage;
	}

	public ArrayList<String> getCheckbox1() {
		return checkbox1;
	}

	public void setCheckbox1(ArrayList<String> checkbox1) {
		this.checkbox1 = checkbox1;
	}

	public ArrayList<String> getCheckbox2() {
		return checkbox2;
	}

	public void setCheckbox2(ArrayList<String> checkbox2) {
		this.checkbox2 = checkbox2;
	}

	public ArrayList<String> getCheckbox3() {
		return checkbox3;
	}

	public void setCheckbox3(ArrayList<String> checkbox3) {
		this.checkbox3 = checkbox3;
	}

	public ArrayList<String> getCheckbox4() {
		return checkbox4;
	}

	public void setCheckbox4(ArrayList<String> checkbox4) {
		this.checkbox4 = checkbox4;
	}

	public void updateCandidatesAnswer() {
		System.out.println("Id ul candidatului 2: "
				+ candidateHistoryController.getCurrentInterviewId());
		System.out.println(ttbr.checkStatus(candidateHistoryController.getCurrentInterviewId()));
		if (ttbr.checkStatus(candidateHistoryController.getCurrentInterviewId()) > 0) {
			String answer = "";

			if (marked.contains(new Integer(page + 1)))
				marked.remove(new Integer(page + 1));

			if (checkbox1.size() != 0)
				answer += checkbox1.get(0);
			if (checkbox2.size() != 0)
				answer += checkbox2.get(0);
			if (checkbox3.size() != 0)
				answer += checkbox3.get(0);
			if (checkbox4.size() != 0)
				answer += checkbox4.get(0);
			System.out.println("answer:" + answer);

			if (!answer.equals(""))
				qdr.updateCandidateAnswer(candidateHistoryController.getCurrentInterviewId(), page, Integer.parseInt(answer));
			else
				qdr.updateCandidateAnswer(candidateHistoryController.getCurrentInterviewId(), page, 0);

			if (goToReview == true) {

				try {

					FacesContext
					.getCurrentInstance()
					.getExternalContext()
					.redirect(
							"/MSGCandidateTesting/faces/reviewquestions.xhtml");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			if (displayedPage == qdr.numberOfQuestions(candidateHistoryController.getCurrentInterviewId()))
				goToReview = true;

			incPage();
			// notTheEnd=false;
		} else {
			finishTest();
		}
	}

	public void markForReview() {
		System.out.println(ttbr.checkStatus(candidateHistoryController
				.getCurrentInterviewId()));
		if (ttbr
				.checkStatus(candidateHistoryController.getCurrentInterviewId()) > 0) {
			if (!marked.contains(new Integer(page + 1)))
				marked.add(new Integer(page + 1));
			if (displayedPage == qdr.numberOfQuestions(candidateHistoryController.getCurrentInterviewId()))
				goToReview = true;
			incPage();
		} else {
			finishTest();
		}
	}

	public void reviewRedirect() {
		System.out.println(ttbr.checkStatus(candidateHistoryController
				.getCurrentInterviewId()));
		if (ttbr
				.checkStatus(candidateHistoryController.getCurrentInterviewId()) > 0) {
			try {
				goToReview = true;
				notTheEnd = false;
				setDone("button");
				FacesContext
				.getCurrentInstance()
				.getExternalContext()
				.redirect(
						"/MSGCandidateTesting/faces/reviewquestions.xhtml");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			finishTest();
		}
	}

	public void setMarked(List<Integer> marked) {
		this.marked = marked;
	}

	public List<Integer> getMarked() {
		return marked;
	}

	public void getMarkedQuestion() {
		Integer item = (Integer) getDataTable().getRowData();
		setPage(item.intValue() - 1);
		setDisplayedPage(item.intValue());
		System.out.println("My page:" + page);
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect(
					"/MSGCandidateTesting/faces/questions.xhtml");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void setDataTable(HtmlDataTable dataTable) {
		this.dataTable = dataTable;
	}

	public HtmlDataTable getDataTable() {
		return dataTable;
	}

	public boolean issMarked() {
		if (marked.contains(new Integer(page + 1)))
			return true;
		return false;
	}

	public void setOk(boolean ok) {
		this.ok = ok;
	}

	public boolean isOk() {
		if (issMarked() || notTheEnd)
			setMark("buttonDis");
		else
			setMark("button");
		return (issMarked() || notTheEnd);
	}

	public void setGoToReview(boolean goToReview) {
		this.goToReview = goToReview;
	}

	public boolean isGoToReview() {
		return goToReview;
	}

	public void setHaveMarked(boolean haveMarked) {
		this.haveMarked = haveMarked;
	}

	public boolean isHaveMarked() {
		if (marked.size() == 0)
			return false;
		return true;
	}

	public void setDontHaveMarked(boolean dontHaveMarked) {
		this.dontHaveMarked = dontHaveMarked;
	}

	public boolean isDontHaveMarked() {
		if (marked.size() == 0)
			return true;
		return false;
	}

	public void goToQuestions() {
		System.out.println(ttbr.checkStatus(candidateHistoryController
				.getCurrentInterviewId()));
		if (ttbr
				.checkStatus(candidateHistoryController.getCurrentInterviewId()) > 0) {
			try {
				goToReview = false;
				FacesContext.getCurrentInstance().getExternalContext()
				.redirect("/MSGCandidateTesting/faces/questions.xhtml");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			finishTest();
		}
	}

	public void setTheEnd(boolean theEnd) {
		this.theEnd = theEnd;
	}

	public boolean isTheEnd() {
		return theEnd;
	}

	public void setNotTheEnd(boolean notTheEnd) {
		this.notTheEnd = notTheEnd;
	}

	public boolean isNotTheEnd() {
		return notTheEnd;
	}

	public void finishTest() {
		System.out.println("Finish test");
		if (ttbr
				.checkStatus(candidateHistoryController.getCurrentInterviewId()) > 0) {
			ttbr.stopTimer(candidateHistoryController.getCurrentInterviewId());
		}
		nrOfMinutes = 0;
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect(
			"/MSGCandidateTesting/faces/finishTest.xhtml");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void setDone(String done) {
		this.done = done;
	}

	public String getDone() {
		return done;
	}

	public void setMark(String mark) {
		this.mark = mark;
	}

	public String getMark() {
		return mark;
	}

	public void setCandidateHistoryController(CandidateHistoryController candidateHistoryController) {
		this.candidateHistoryController = candidateHistoryController;
	}

	public CandidateHistoryController getCandidateHistoryController() {
		
		return candidateHistoryController;
	}

	public void setPagebottom(String pagebottom) {
		this.pagebottom = pagebottom;
	}
	
	public String getPagebottom() {
		System.out.println("yayaaaa");
		pagebottom=displayedPage + " out of " + qdr.numberOfQuestions(candidateHistoryController.getCurrentInterviewId());
		return pagebottom;
	}

	public void setInputHidden(String inputHidden) {
		this.inputHidden = inputHidden;
	}

	public String getInputHidden() {
		return inputHidden;
	}

	public void setNrOfMinutes(int nrOfMinutes) {
		this.nrOfMinutes = nrOfMinutes;
	}

	public int getNrOfMinutes() {
		return nrOfMinutes;
	}
}
